<template>
  <div class="user-audit-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>待审核用户列表</span>
        </div>
      </template>
      
      <el-table :data="pendingUsers" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="telephone" label="电话" width="120" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="registerTime" label="注册时间" width="180" />
        <el-table-column prop="auditStatus" label="审核状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.auditStatus === 0 ? 'warning' : scope.row.auditStatus === 1 ? 'success' : 'danger'">
              {{ scope.row.auditStatus === 0 ? '待审核' : scope.row.auditStatus === 1 ? '已通过' : '已拒绝' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="auditRemark" label="审核备注" width="200" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button
              size="small"
              type="success"
              @click="handleApprove(scope.row)"
              :disabled="scope.row.auditStatus !== 0"
            >
              通过
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleReject(scope.row)"
              :disabled="scope.row.auditStatus !== 0"
            >
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 审核备注对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="30%">
      <el-form :model="auditForm">
        <el-form-item label="审核备注">
          <el-input
            v-model="auditForm.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入审核备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAudit">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from '@/utils/axios'

export default {
  name: 'UserAuditView',
  setup() {
    const pendingUsers = ref([])
    const currentPage = ref(1)
    const pageSize = ref(10)
    const total = ref(0)
    const dialogVisible = ref(false)
    const dialogTitle = ref('')
    const auditForm = ref({
      userId: null,
      status: 0,
      remark: ''
    })

    // 获取待审核用户列表
    const fetchPendingUsers = async () => {
      try {
        const response = await axios.get('/user/selectPending', {
          params: {
            current: currentPage.value,
            size: pageSize.value
          }
        })
        if (response.data.code === 200) {
          pendingUsers.value = response.data.result.records || []
          total.value = response.data.result.total || 0
        } else {
          ElMessage.error(response.data.msg || '获取待审核用户列表失败')
        }
      } catch (error) {
        console.error('获取待审核用户列表失败:', error)
        ElMessage.error('获取待审核用户列表失败')
      }
    }

    // 处理审核通过
    const handleApprove = (row) => {
      dialogTitle.value = '审核通过'
      auditForm.value = {
        userId: row.id,
        status: 1,
        remark: ''
      }
      dialogVisible.value = true
    }

    // 处理审核拒绝
    const handleReject = (row) => {
      dialogTitle.value = '审核拒绝'
      auditForm.value = {
        userId: row.id,
        status: 2,
        remark: ''
      }
      dialogVisible.value = true
    }

    // 提交审核
    const submitAudit = async () => {
      try {
        const response = await axios.post(`/user/audit/${auditForm.value.userId}`, null, {
          params: {
            auditStatus: auditForm.value.status,
            auditRemark: auditForm.value.remark
          }
        })
        if (response.data.code === 200) {
          ElMessage.success('审核操作成功')
          dialogVisible.value = false
          fetchPendingUsers()
        } else {
          ElMessage.error(response.data.msg || '审核操作失败')
        }
      } catch (error) {
        console.error('审核操作失败:', error)
        ElMessage.error('审核操作失败')
      }
    }

    // 处理页码改变
    const handleCurrentChange = (val) => {
      currentPage.value = val
      fetchPendingUsers()
    }

    // 处理每页条数改变
    const handleSizeChange = (val) => {
      pageSize.value = val
      currentPage.value = 1
      fetchPendingUsers()
    }

    onMounted(() => {
      fetchPendingUsers()
    })

    return {
      pendingUsers,
      currentPage,
      pageSize,
      total,
      dialogVisible,
      dialogTitle,
      auditForm,
      handleApprove,
      handleReject,
      submitAudit,
      handleCurrentChange,
      handleSizeChange
    }
  }
}
</script>

<style scoped>
.user-audit-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>